/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.close.alarm.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.hopes.bizz.close.alarm.entity.ClosedAlarmHandleRecordEntity;
import com.hopes.bizz.close.alarm.dto.ClosedAlarmHandleRecordExcelDTO;
import com.hopes.bizz.close.alarm.vo.ClosedAlarmHandleRecordExcelVO;
import com.hopes.bizz.close.alarm.mapper.ClosedAlarmHandleRecordMapper;
import com.hopes.bizz.close.alarm.service.ClosedAlarmHandleRecordService;
import com.hopes.bizz.close.common.constant.CloseModuleEnums;
import com.hopes.bizz.close.common.constant.HazardDicConstant;
import com.hopes.bizz.close.hazard.entity.ClosedAlarmHazardCarEntity;
import com.hopes.bizz.close.hazard.service.ClosedAlarmHazardCarService;
import com.hopes.boss.common.core.exception.BizException;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
/**
 * 报警处理记录
 *
 * @author pig
 * @date 2024-02-01 11:00:12
 */
@Service
@RequiredArgsConstructor
public class ClosedAlarmHandleRecordServiceImpl extends ServiceImpl<ClosedAlarmHandleRecordMapper, ClosedAlarmHandleRecordEntity> implements ClosedAlarmHandleRecordService {

    private final ClosedAlarmHazardCarService closedAlarmHazardCarService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(ClosedAlarmHandleRecordEntity closedAlarmHandleRecord) {
        final Long moduleId = closedAlarmHandleRecord.getModuleId();
        final String moduleCode = closedAlarmHandleRecord.getModuleCode();
        if (CloseModuleEnums.CloseAlarmEnum.hazard_car.name().equals(moduleCode)) {
            final ClosedAlarmHazardCarEntity alarmHazardCar = closedAlarmHazardCarService.getById(moduleId);
            if (ObjUtil.isNull(alarmHazardCar)) {
                throw new BizException("获取告警数据失败！");
            }
            closedAlarmHandleRecord.setAlarmTime(alarmHazardCar.getAlarmTime());
            closedAlarmHandleRecord.setAlarmContent(alarmHazardCar.getAlarmContent());

            closedAlarmHazardCarService.lambdaUpdate()
                    .set(ClosedAlarmHazardCarEntity::getHandleTime, closedAlarmHandleRecord.getHandleTime())
                    .set(ClosedAlarmHazardCarEntity::getHandleResult, closedAlarmHandleRecord.getHandleResult())
                    .set(ClosedAlarmHazardCarEntity::getHandlePerson, closedAlarmHandleRecord.getHandlePerson())
                    .set(ClosedAlarmHazardCarEntity::getHandleStatus, HazardDicConstant.CLOSE_HAZARD_ALARM_HANDLE_STATUS_DONE)
                    .eq(ClosedAlarmHazardCarEntity::getId, closedAlarmHandleRecord.getModuleId()).update();
        }
        return SqlHelper.retBool(getBaseMapper().insert(closedAlarmHandleRecord));
    }


    @Override
    public ValidateImportBO<ClosedAlarmHandleRecordExcelDTO> validateImport(List<ClosedAlarmHandleRecordExcelDTO> excelDTOList) {
        ValidateImportBO<ClosedAlarmHandleRecordExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<ClosedAlarmHandleRecordExcelDTO> saveList = new ArrayList<>(excelDTOList);
        int successNum = excelDTOList.size();
        int failedNum = 0;

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public ClosedAlarmHandleRecordEntity importDtoToEntity(ClosedAlarmHandleRecordExcelDTO excelDto) {
        ClosedAlarmHandleRecordEntity entity= new ClosedAlarmHandleRecordEntity();
        BeanUtils.copyProperties(excelDto, entity);
        return entity;
    }

    @Override
    public ClosedAlarmHandleRecordExcelVO exportEntityToVo(ClosedAlarmHandleRecordEntity entity) {
        ClosedAlarmHandleRecordExcelVO excelVO = new ClosedAlarmHandleRecordExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }
}
